package com.giantlizard.cloud.base.entity;


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "分页对象", description = "分页对象")
@JsonIgnoreProperties(ignoreUnknown = true)
public class PageQuery implements Serializable {
    /**
     * 当前页码
     */
    @ApiModelProperty(value = "当前页码")
    private Integer pageNum = 1;

    /**
     * 每页记录数
     */
    @ApiModelProperty(value = "页大小")
    private Integer pageSize = 15;

    /**
     * 排序类型
     */
    @ApiModelProperty(value = "排序方式，ASC升序、DESC降序")
    private String orderType;

    /**
     * 排序字段
     */
    @ApiModelProperty(value = "排序字段")
    private String orderBy;


    /**
     * 总行数
     */
    @ApiModelProperty(value = "总记录数，无需前端传递")
    private Long totalRows;
    /**
     * 总页数
     */
    @ApiModelProperty(value = "总页数，无需前端传递")
    private Long totalPages;
    /**
     * 查询结果列表
     */
    @ApiModelProperty(value = "返回数据列表")
    private List resultList = new ArrayList();

    /**
     * 获取开始位置索引
     *
     * @return StartIndex
     */
    public Integer getStartIndex() {
        return (this.pageNum - 1) * this.pageSize + 1;
    }

    /**
     * 获取开始位置
     *
     * @return start
     */
    public Integer getStart() {
        return (this.pageNum - 1) * this.pageSize + 1;
    }

    /**
     * 获取结束位置
     *
     * @return end
     */
    public Integer getEnd() {
        return this.pageNum * this.pageSize;
    }

    /**
     * 设置总记录数
     *
     * @param totalRows 总记录数
     */
    public void setTotalRows(Long totalRows) {
        this.totalRows = totalRows;
        //计算页总数
        this.totalPages = totalRows / getPageSize() + (totalRows % getPageSize() == 0 ? 0 : 1);
    }

    /**
     * 设置页大小
     *
     * @param pageSize 页大小
     */
    public void setPageSize(Integer pageSize) {
        if (pageSize == null || pageSize < 1) {
            this.pageSize = 15;
        } else {
            this.pageSize = pageSize;
        }
    }
}
